//
//  ModelArray.h
//
//  Created by Molon on 13-10-25.
//  Copyright (c) 2013年 Molon. All rights reserved.
//

#import <Foundation/Foundation.h>

//因为NSMutableArray不适宜继承，所以不能用其作为父类
@interface ModelArray : NSObject<NSFastEnumeration,NSCopying>

@property (nonatomic,unsafe_unretained,readonly) Class modelClass;

@property (nonatomic,assign,readonly) NSInteger count;
//实际存储对象的Array
@property (nonatomic,strong,readonly) NSArray *array;

+ (instancetype)turnObject:(NSArray*)array;
- (void)turnObject:(NSArray*)array;


/**
 *  检查是否是设置的Model对象
 */
- (BOOL)isObjectValid:(id)anObject;

/**
 *  获取每个Model元素的某个属性的值整理成的数组，可选是否忽略nil
 */
- (NSArray*)arrayWithElementsOfModelObjectForElementKey:(NSString*)key;
- (NSArray*)arrayWithElementsOfModelObjectForElementKey:(NSString*)key isIgnoreNil:(BOOL)isIgnoreNil;

/**
 *  根据Model的key，和当前数组里key重复的元素删除，一般也就懒加载的需求下使用
 */
- (void)removeObjectsWithModelArray:(ModelArray*)modelArray byKey:(NSString*)key;

/**
 *  根据key equal去重
 */
- (void)removeDuplicateObjectsByKey:(NSString *)key;

/**
 *  转化成array，可以被直接转换成JSON的，内部Model会处理成Dictionary
 */
- (NSArray*)convertedArrayWithIgnoreNil:(BOOL)ignoreNil;

/**
 *  convertedArrayWithIgnoreNil:返回的数组转换成的JSON
 */
- (NSString *)JSONStringWithIgnoreNil:(BOOL)ignoreNil;

/**
 *  需要继承，也可不继承，默认是认作 XXXArray 的对应modelClass是XXX
 */
- (Class)configureModelClass;


//模拟NSArray的一些常用方法
- (NSInteger)count;
- (id)objectAtIndex:(NSUInteger)index;
- (id)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);//使得可以使用[index]语法
- (BOOL)containsObject:(id)anObject;
- (NSUInteger)indexOfObject:(id)anObject;

//模拟NSMutableArray的一些常用方法
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeAllObjects;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);//使得可以使用[index]=??语法

- (void)removeObject:(id)object;
- (void)removeObjectsInArray:(NSArray *)otherArray;
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;

- (void)addObjectsFromArray:(NSArray *)otherArray;
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;

- (id)firstObject;
- (id)lastObject;

@end
